package com.yq.order;

import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * Created by yinqi on 2018/9/1.
 */
public class Request {

    private static String ORDER_NO = "107427227";
    private static DecimalFormat df = new DecimalFormat("#0000");
    private static RequestPresenter requestPresenter;

    public static void main(String[] args) throws Exception {
        requestPresenter = new RequestPresenter();
//        OcrPresenter ocrPresenter = new OcrPresenter();
//        requestPresenter.getHtml();
//        requestPresenter.startWork(0);
//        String path = "D:\\ems\\test.png";
//        ocrPresenter.getOcrResult(path);

//        Map<String, String> paramsMap = new HashMap<String, String>(2);
//        paramsMap.put("mailNum", "11111111");
//        paramsMap.put("checkCode", "111111");
//        requestPresenter.queryOrder(paramsMap);
        ExecutorService service = new ScheduledThreadPoolExecutor(10);
//        for (int i = 0; i < 5; i++) {
//            Thread.sleep(1000);
//            service.submit(new MyRunnable(i));
//        }
//        requestPresenter.startWork(ORDER_NO + "0030");
//        new Thread(new MyRunnable(422, 3000)).start();
        new Thread(() -> checkOrrderNo()).start();
    }

    private static class MyRunnable implements Runnable {
        int startNum;
        int endNum;

        public MyRunnable(int startNum, int endNum) {
            this.startNum = startNum;
            this.endNum = endNum;
        }

        public void run() {
            for (int i = startNum; i < endNum; i++) {
                requestPresenter.startWork(ORDER_NO + df.format(i));
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void checkOrrderNo() {
        RequestPresenter presenter = new RequestPresenter();
        List<String> orderList = getOrderList();
        for (int i = 0; i < orderList.size(); i++) {
            presenter.startWork(orderList.get(i));
        }
    }

    private static List<String> getOrderList() {
        List<String> orderList = new ArrayList<String>();
        try {
            FileReader reader = new FileReader("D:/ems/error.txt");
            BufferedReader bfr = new BufferedReader(reader);
            String line = null;
            while (null != (line = bfr.readLine())) {
                if (line.contains("验证码校验失败"))
                    orderList.add(line.substring(0, 13));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return orderList;
    }
}
